<template>
  <canvas id="CommonGraphicVerificationCodeGeneratorCanvas"
    class="common-graphic-verification-code-generator"
    @click="getGraphicVerificationCode">
  </canvas>
</template>

<script>
export default {
  // 通用图形验证码生成器组件
  name: 'CommonGraphicVerificationCodeGenerator',
  methods: {
    getGraphicVerificationCode () {
      var showNum = []
      // document.getElementById('canvas').style.width;
      var canvasWidth = 200
      // document.getElementById('canvas').style.height;
      var canvasHeight = 50
      // 获取到canvas的对象，景色
      // var canvas = document.getElementById('canvas')
      var canvas = document.getElementById('CommonGraphicVerificationCodeGeneratorCanvas')
      // 获取到canvas画图的环境，景色呈现的画布
      var context = canvas.getContext('2d')
      canvas.width = canvasWidth
      canvas.height = canvasHeight
      var sCode =
        'A,B,C,E,F,G,H,J,K,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0'
      var aCode = sCode.split(',')
      // 获取到数组的长度
      var aLength = aCode.length
      for (let i = 0; i <= 3; i++) {
        // 获取到随机的索引值
        let j = Math.floor(Math.random() * aLength)
        // 产生0~30之间的随机弧度
        let deg = (Math.random() * 30 * Math.PI) / 180
        // 得到随机的一个内容
        let txt = aCode[j]
        showNum[i] = txt.toLowerCase()
        // 文字在canvas上的x坐标
        let x = 10 + i * 20
        // 文字在canvas上的y坐标
        let y = 20 + Math.random() * 8
        context.font = 'bold 23px 微软雅黑'

        context.translate(x, y)
        context.rotate(deg)

        context.fillStyle = this.randomColor()
        context.fillText(txt, 0, 0)

        context.rotate(-deg)
        context.translate(-x, -y)
      }
      for (let i = 0; i <= 5; i++) {
        // 验证码上显示线条
        context.strokeStyle = this.randomColor()
        context.beginPath()
        context.moveTo(
          Math.random() * canvasWidth,
          Math.random() * canvasHeight
        )
        context.lineTo(
          Math.random() * canvasWidth,
          Math.random() * canvasHeight
        )
        context.stroke()
      }
      for (let i = 0; i <= 30; i++) {
        // 验证码上显示小点
        context.strokeStyle = this.randomColor()
        context.beginPath()
        let x = Math.random() * canvasWidth
        let y = Math.random() * canvasHeight
        context.moveTo(x, y)
        context.lineTo(x + 1, y + 1)
        context.stroke()
      }
      // 将showName的数组拼接成一个字符串
      let verificationCodeValue = ''
      showNum.forEach((item, index) => {
        verificationCodeValue += item
      })
      this.$emit('getVerificationCodeValue', verificationCodeValue)
    },
    randomColor () {
      // 得到随机的颜色值
      let r = Math.floor(Math.random() * 256)
      let g = Math.floor(Math.random() * 256)
      let b = Math.floor(Math.random() * 256)
      return 'rgb(' + r + ',' + g + ',' + b + ')'
    }
  },
  mounted () {
    this.getGraphicVerificationCode()
  }
}
</script>

<style lang="stylus" scoped>
.common-graphic-verification-code-generator
  border-style solid
  border-width 1px
  border-color gainsboro
</style>
